package com.kelvin.manage.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kelvin.manage.common.CommonResult;
import com.kelvin.manage.mapper.StudentMapper;
import com.kelvin.manage.model.Student;
import com.kelvin.manage.service.TeacherService;
import com.kelvin.manage.service.impl.TeacherServiceimpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@Controller
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private TeacherServiceimpl teacherServiceimpl;

    // 删除学生
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Integer> deleteStudent(@RequestParam long studentId) {
        int result = teacherServiceimpl.deleteStudent(studentId);
        if (result > 0) {
            return new CommonResult<>(200, "删除成功", result);
        } else {
            return new CommonResult<>(500, "删除失败或记录不存在", result);
        }
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Integer> updateStudent(@RequestBody Student student) {
        int result = studentMapper.updateStudent(student);
        if (result > 0) {
            return new CommonResult<>(200, "更新成功", result);
        } else {
            return new CommonResult<>(500, "更新失败或数据未变化", result);
        }
    }

    @RequestMapping(value="/students",method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<Student>> getAllStudent(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                     @RequestParam(value = "pageNum", defaultValue = "1")Integer pageNum){
        PageHelper.startPage(pageNum, pageSize);
        List<Student> students=studentMapper.getAllStudent();
        return new CommonResult(200,"获取成功", new PageInfo(students));
    }

    @RequestMapping(value="/student",method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Student> getStudent(@RequestParam long id){
        Student student=studentMapper.getStudentById(id);
        return new CommonResult(200,"获取成功", student);
    }

//    @RequestMapping(value="/uploadStudents",method = RequestMethod.POST)
//    @ResponseBody
//    public CommonResult<String> uploadStudents(@RequestParam("file") MultipartFile file) {
//        if (file.isEmpty()) {
//            return new CommonResult<>(400, "文件为空", null);
//        }
//
//        try {
//            List<Student> studentList = EasyExcel.read(file.getInputStream())
//                    .head(Student.class)
//                    .sheet()
//                    .doReadSync();
//
//            if (studentList.isEmpty()) {
//                return new CommonResult<>(400, "Excel中没有数据", null);
//            }
//
//            for (Student student : studentList) {
//                studentMapper.insertStudent(student);
//            }
//
//            log.info("成功导入{}个学生记录", studentList.size());
//            return new CommonResult<>(200, "导入成功，共" + studentList.size() + "条数据", null);
//
//        } catch (Exception e) {
//            log.error("Excel文件导入失败", e);
//            return new CommonResult<>(500, "文件解析或导入失败", null);
//        }
//    }

}
